package cn.analysys.controller;

import cn.analysys.exception.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author jiangbang
 * @date 2017/4/13
 */
@Controller
public class BaseController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }

    /**
     * 统一异常处理，返回页面
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/error/html")
    public String hello2() throws Exception {
        throw new Exception("发生错误");
    }

    /**
     * 统一异常处理，返回json
     * @return
     * @throws Exception
     */
    @RequestMapping("/error/json")
    public String json() throws MyException {
        throw new MyException("发生错误2");
    }

    @RequestMapping("/index")
    public String index(ModelMap map){
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://www.baidu.com");
        map.addAttribute("name", "进入百度");
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";
    }

}
